package com.wanghua.mall.order.controller;

import com.wanghua.common.utils.R;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author wanghua
 * @create 2021-09-07 19:56
 * 向消息队列中发送消息
 */
@RestController
public class RabbitController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg")
    public R sendMsg(@RequestParam(value = "num", required = false, defaultValue = "10") Integer num) {
        List<Integer> list = Arrays.asList(1, 2, 3);
        Set<Integer> set = new HashSet<>(list);
        for (int i = 0; i < num; i++) {
            if (i % 2 == 0) {
                rabbitTemplate.convertAndSend("java.exchange", "java", list, new CorrelationData("11111"));
            } else {
                rabbitTemplate.convertAndSend("java.exchange", "java", set, new CorrelationData("22222"));
            }
        }
        return R.ok();
    }

}
